package com.wit.example.util;

import android.content.Context;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wit.example.UI.RegisterActivity;
import com.wit.example.pojo.User;
import com.wit.example.share.NetConstant;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.MediaType;
import okhttp3.Callback;
import okhttp3.Response;


/**
* 网络传输工具类  
*  
* @Author liaozhaoping 
* @Date 2024/5/28 10:09
*/
public class NetUtil {
    private static final OkHttpClient client = new OkHttpClient();

    /**
     * 发送异步POST请求。
     *
     * @param url       请求的URL
     * @param json      要发送的JSON数据
     * @param callback  处理响应的回调接口
     */
    public static void sendPostAsync(String url, String json, Callback callback) {
        RequestBody body = RequestBody.create(json, MediaType.get("application/json; charset=utf-8"));
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        // 正确的调用方式
        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    /**
     * 发送异步GET请求。
     *
     * @param url       请求的URL
     * @param callback  处理响应的回调接口
     */
    public static void sendGetAsync(String url, Callback callback) {
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        // 正确的调用方式
        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    /**
    *   将用户信息发送到服务器
    *
    * @Author liaozhaoping
    * @Date 2024-08-16 15:31
    */

    public static void sendUserInfoToServer(Context context,User user) {
        OkHttpClient client = new OkHttpClient();

        // 将 UserInfo 对象转换为 JSON 字符串
        Gson gson = new Gson();
        String json = gson.toJson(user);

        // 创建 RequestBody
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);

        // 创建 Request
        Request request = new Request.Builder()
                .url(NetConstant.USER_REGISTER_URL) //注册url
                .post(requestBody)
                .build();

        // 发送异步请求
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                // 请求失败的处理
                e.printStackTrace();
                ToastUtil.showToast(context, "注册失败,请稍后重试");

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    // 请求成功的处理
                    //提示用户注册成功
                    ToastUtil.showToast(context, "恭喜你注册成功");
                    String responseData = response.body().string();
                    // 处理服务器返回的数据
                    // ...
                } else {
                    // 请求失败的处理
                    // ...
                    ToastUtil.showToast(context, "注册失败,请稍后重试");
                }
            }
        });
    }

    /**
    *  验证用户的用户名和密码是否正确
    *
    * @Author liaozhaoping
    * @Date 2024-08-22 14:22
    */
    public static void verifyUserCredentials(Context context, String username, String password, Callback callback) {
        // Create a map to hold the username and password
        Map<String, String> credentials = new HashMap<>();
        credentials.put("username", username);
        credentials.put("password", password);

        // Convert the map to a JSON string
        Gson gson = new Gson();
        String json = gson.toJson(credentials);

        // Send a POST request to the server
        sendPostAsync(NetConstant.USER_LOGIN_URL, json, callback);
    }


}
